package cn.dckeji.gameBox.controller;


import cn.dckeji.gameBox.common.utils.DataUtil;
import cn.dckeji.gameBox.common.utils.ResultUtil;
import cn.dckeji.gameBox.entity.Loginlog;
import cn.dckeji.gameBox.entity.User;
import cn.dckeji.gameBox.service.LoginlogService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2018-08-20
 */
@RestController
@RequestMapping("/loginlog")
public class LoginlogController {
    @Autowired
    private LoginlogService loginlogService;

    /**
     * 获取当天24小时的登录用户
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("getLoginLogList")
    public Map<String, Object> getLoginLogList(Integer page, Integer limit){
        Page<Loginlog> loginlogPage = new Page<>();
        loginlogPage.setSize(limit);
        loginlogPage.setCurrent(page);
        EntityWrapper<Loginlog> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("type",1);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String data=sdf.format(new Date());
        entityWrapper.orderBy("loginTime",false);
        entityWrapper.between("loginTime",data+" 00:00:00",data+" 23:59:59");
        loginlogPage = loginlogService.selectPage(loginlogPage,entityWrapper);
        return ResultUtil.resultPage(loginlogPage.getTotal(),loginlogPage.getRecords());
    }

    /**
     * 获取启动盒子日志
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("getStartBoxList")
    public Map<String, Object> getStartBoxList(Integer page, Integer limit,String userName,String date){
        Page<Loginlog> loginlogPage = new Page<>();
        loginlogPage.setSize(limit);
        loginlogPage.setCurrent(page);
        EntityWrapper<Loginlog> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("type",1);
        entityWrapper.orderBy("loginTime",false);
        if(StringUtils.isNotBlank(userName)){
            entityWrapper.like("userName",userName);
        }
        if(StringUtils.isNotBlank(date)){
            if(StringUtils.isNotBlank(date)){
                String starttime = date.substring(0, 11);
                String enddate = date.substring(date.length() - 10);
                entityWrapper.between("loginTime",starttime+" 00:00:00",enddate+" 23:59:59");
            }
        }
        loginlogPage = loginlogService.selectPage(loginlogPage,entityWrapper);
        return ResultUtil.resultPage(loginlogPage.getTotal(),loginlogPage.getRecords());
    }

    /**
     * 获取访问游戏日志
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("getStartGameList")
    public Map<String, Object> getStartGameList(Integer page, Integer limit,String gameName,String userName,String date){
        Page<Loginlog> loginlogPage = new Page<>();
        loginlogPage.setSize(limit);
        loginlogPage.setCurrent(page);
        EntityWrapper<Loginlog> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("type",2);
        entityWrapper.orderBy("loginTime",false);
        if(StringUtils.isNotBlank(gameName)){
            entityWrapper.like("gameName",gameName);
        }
        if(StringUtils.isNotBlank(userName)){
            entityWrapper.like("userName",userName);
        }
            if(StringUtils.isNotBlank(date)){
                String starttime = date.substring(0, 11);
                String enddate = date.substring(date.length() - 10);
                entityWrapper.between("loginTime",starttime+" 00:00:00",enddate+" 23:59:59");
            }
        loginlogPage = loginlogService.selectPage(loginlogPage,entityWrapper);
        return ResultUtil.resultPage(loginlogPage.getTotal(),loginlogPage.getRecords());
    }
    /**
     * 获取启动盒子日志
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("getContinueList")
    public Map<String, Object> getContinueList(Integer page, Integer limit,Integer type,String userName){
        Page<Map<String,Object>> loginlogPage = new Page<>();
        loginlogPage.setSize(limit);
        loginlogPage.setCurrent(page);
        Map<String,Object> maps=new HashMap<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String beginTime=sdf.format(new Date());
        String endTime=null;
        if(type!=null&&2==type){
            endTime =DataUtil.getPastDate(7);
            maps.put("index",6);
        }else{
            endTime =DataUtil.getPastDate(2);
            maps.put("index",1);
        }
        maps.put("beginTime",endTime+" 00:00:00");
        maps.put("endTime",beginTime+" 23:59:59");
        if(StringUtils.isNotBlank(userName)){
            maps.put("userName",userName);
        }
        maps.put("pageNum",page);
        maps.put("current",limit);
        loginlogPage=loginlogService.getLoginLogContinue(maps);
        return ResultUtil.resultPage(loginlogPage.getTotal(),loginlogPage.getRecords());
    }



}

